<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="formInline.name"
          placeholder="请输入角色名称"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="onSubmit"
          size="mini"
          icon="el-icon-search"
          >查找</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="addopen"
          size="mini"
          icon="el-icon-edit"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <div class="tables">
      <template>
        <el-table border :data="tableData" style="width: 100%">
          <el-table-column prop="name" label="角色名称" align="center" sortable>
          </el-table-column>
          <el-table-column prop="desc" label="说明" align="center">
          </el-table-column>
          <el-table-column prop="address" label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
              >
                编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="dels(scope.$index, scope.row)"
              >
                删除
              </el-button>
              <el-button
                type="primary"
                size="mini"
                @click="accredit(scope.row)"
              >
                授权
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="get.page"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="get.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- ========================= -->
    <el-dialog title="编辑" :visible.sync="dialogVisible" width="50%">
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="角色名称" prop="name">
          <el-input
            v-model="form.name"
            style="width: 300px"
            size="mini"
          ></el-input>
        </el-form-item>
        <el-form-item label="说明">
          <el-input
            v-model="form.desc"
            style="width: 300px"
            size="mini"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="nono('form')">取 消</el-button>
        <el-button v-if="flag" type="primary" @click="update('form')"
          >确 定</el-button
        >
        <el-button v-if="!flag" type="primary" @click="addlist('form')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- ============================= -->
    <el-dialog title="权限配置" :visible.sync="dialogVisible2" width="50%">
      <el-tree
        :data="data"
        :props="defaultProps"
        show-checkbox
        node-key="id"
        :default-checked-keys="att"
        @check="handleCheckChange"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span
          ><el-tag
            v-if="node.isLeaf"
            size="mini"
            :type="data.api ? 'success' : 'primary'"
            ><span>{{ data.api }}</span></el-tag
          ></span
        >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false" size="mini">取 消</el-button>
        <el-button size="mini" type="primary" @click="grantlist"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getList, redact, del, add, grant, updatetx } from "@/api/systemm/user";
export default {
  data() {
    return {
      roleId: "",
      attid: [],
      att: [],
      data: [{}],
      defaultProps: {
        children: "children",
        label: "label",
      },
      flag: true,
      formLabelWidth: "120px",
      dialogVisible2: false,
      dialogVisible: false,
      form: {},
      tableData: [],
      formInline: {},
      total: 0,
      get: {
        page: 1,
        limit: 20,
        name: "",
        sort: "add_time",
        order: "desc",
      },
      rules: {
        name: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getdata();
  },
  methods: {
    //数据获取
    getdata() {
      getList(this.get).then((res) => {
        // console.log(res);
        if ((res.data.errmsg = "成功")) {
          this.tableData = res.data.list;
          this.total = res.data.total;
          // console.log(this.tableData);
        }
      });
    },
    // 查询
    onSubmit() {
      this.get.page = 1;
      this.get.name = this.formInline.name;
      this.getdata();
    },

    // 编辑
    handleEdit(a, b) {
      this.flag = true;
      this.dialogVisible = true;
      this.form = b;
    },
    //编辑确定
    update(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          redact(this.form).then((res) => {
            if (res.errmsg == "成功") {
              // console.log(res);
              this.getdata();
              this.form = {};
              this.dialogVisible = false;
              this.$notify({
                title: "成功",
                message: "更新成功",
                type: "success",
              });
            }
          });
        }
      });
    },
    //编辑取消
    nono(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    // 删除
    dels(a, b) {
      del(b).then((res) => {
        if (res.errmsg == "成功") {
          this.getdata();
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
          });
        }
      });
    },
    // 添加打开
    addopen(formName) {
      this.flag = false;
      this.dialogVisible = true;
    },
    //添加
    addlist(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          add(this.form).then((res) => {
            // console.log(res);
            if (res.errmsg == "成功") {
              this.getdata();
              this.form = {};
              this.dialogVisible = false;
              this.$notify({
                title: "成功",
                message: "添加成功",
                type: "success",
              });
            } else if (res.errmsg == "角色已经存在") {
              this.$notify({
                title: "失败",
                message: res.errmsg,
                type: "error",
              });
            }
          });
        }
      });
    },
    // 授权
    accredit(row) {
      // console.log(row);
      this.dialogVisible2 = true;
      this.roleId = row.id;
      grant(this.roleId).then((res) => {
        // console.log(res.data.systemPermissions);
        this.data = res.data.systemPermissions;
        this.att = res.data.assignedPermissions;
        // console.log(this.data, this.att);
      });
    },
    // 授权确定
    grantlist() {
      let data = new Object();
      data.roleId = this.roleId;
      data.permissions = this.attid;
      // console.log(data);
      updatetx(data).then((res) => {
        if (res.errmsg == "成功") {
          this.$notify({
            title: "成功",
            message: "授权成功",
            type: "success",
          });
          this.dialogVisible2 = false;
        }
      });
    },
    //分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.get.limit = val;
      this.getdata();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.get.page = val;
      this.getdata();
    },
    // 树形控件
    handleCheckChange(data, checked) {
      // console.log(checked.checkedKeys);
      this.attid = checked.checkedKeys;
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-table td,
::v-deep .el-table th {
  padding: 4px 0px;
}
.el-table {
  font-size: 12px;
}
.page {
  margin-top: 40px;
  display: flex;
  justify-content: left;
}
</style>